import {Token}                  from '../utils/Storage';
import {DateFormat}             from '../utils/Format';
import {transVoucher}           from "./voucher";
import {CALL_API, STORE}        from "../constants";
import {postMethod, transform}  from "../utils/fetch";

//获取积分信息
const fetchAccum = () => ({
    [CALL_API]: {
        url: `VMembers/selectJifenByMId?m_id=${Token.getUserId()}`,
        types: STORE.accum.types(),
        extra: r => {
            var {kkhcode, jifen, HYklMap, crsq, nextGrade, nextGrade_need} = r.data;
            return Object.assign(r, {
                data: {
                    qr: kkhcode,
                    time: crsq,
                    text: nextGrade_need === 0 ? '已是顶级vip' : `成为${nextGrade}卡会员还有${nextGrade_need}元的距离`,
                    accumulate: jifen,
                    card: {
                        name: HYklMap.sign,
                        discount: HYklMap.koulv,
                    },
                }
            });
        }
    }
});

//积分列表
const fetchAccumList = ({index, size}) => ({
    [CALL_API]: {
        url: `VMembers/selectSelldiaryByMId?m_id=${Token.getUserId()}&page=${index}&rows=${size}`,
        types: STORE.accumList.types(),
        extra: transform(
            ({
                 changetime,
                 changetype,
                 changescore,
             }, id) =>
                ({
                    id,
                    time: DateFormat.format(changetime),
                    title: changetype,
                    accumulate: changescore,
                })
        )
    }
});

//上架列表
const fetchInList = ({index, size}) => ({
    [CALL_API]: {
        url: `VMembers/selectAllVVouchers?page=${index}&rows=${size}`,
        types: STORE.inList.types(),
        extra: transform(transVoucher)
    }
});

//下架列表
const fetchOutList = ({index, size}) => ({
    [CALL_API]: {
        url: `VMembers/selectVVoucherschangelog?page=${index}&rows=${size}`,
        types: STORE.outList.types(),
        extra: transform(
            ({
                 id,
                 spmch,
                 shllast,
                 kkhname,
                 shlcount,
                 changetime,
             }) => ({
                id,
                own: kkhname,
                name: spmch,
                time: DateFormat.format(changetime),
                count: shlcount - shllast,
                total: shlcount,
            })
        )
    }
});

//下架
const fetchOut = ({vId, expire}) => ({
    [CALL_API]: {
        url: 'VMembers/updateVVouchersTo0',
        types: STORE.out.types(),
        options: postMethod({
            spid: vId,
            sxrq: expire,
            m_id: Token.getUserId()
        })
    }
});

//修改
const fetchEdit = ({vId, expire, principle, rule, range}) => ({
    [CALL_API]: {
        url: 'VMembers/updateVVouchersSY',
        types: STORE.edit.types(),
        options: postMethod({
            spid: vId,
            sxrq: expire,
            jfyz: principle,
            jfnr: rule,
            jfsysd: range,
        })
    }
});

export const out = entity                           => dispatch => dispatch(fetchOut(entity));
export const edit = entity                          => dispatch => dispatch(fetchEdit(entity));
export const loadAccum = ()                         => dispatch => dispatch(fetchAccum());
export const loadInList = page                      => dispatch => dispatch(fetchInList(page));
export const loadOutList = page                     => dispatch => dispatch(fetchOutList(page));
export const loadAccumList = page                   => dispatch => dispatch(fetchAccumList(page));
